<template>
  <div class="login">
    <div class="name">分行者跟单社区</div>
    <div class="small">MT4/MT5跟单系统社区一站式服务</div>
    <el-form class="login_form" ref="form" :rules="rules" :model="form" label-width="0px">
      <el-form-item prop="phone">
        <el-input size="small" placeholder="手机号/邮箱" prefix-icon="el-icon-user" v-model="form.phone">
      </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input size="small" placeholder="登录密码" prefix-icon="el-icon-lock" v-model="form.password" show-password>
        </el-input>
      </el-form-item>
      <el-form-item>
        <div class="option">
          <div class="is_login" @click.stop="changeLogin">
            <!-- <div class="icon_box" :style="{ color: isLogin ? '#04d1b3' : '#fff' }">
              <input type="checkbox" class="auto-login" v-model="autoLogin">
            </div>
            <span class="ft-14">自动登录</span> -->
            <el-checkbox v-model="autoLogin" :checked="isChecked"><span style="color: black;">自动登录</span> </el-checkbox>
          </div>
          <div class="link">
            <router-link class="link_item " to="register">注册账户</router-link>
            <router-link class="link_item" to="forget">忘记密码</router-link>
          </div>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="success" class="ft-14" size="small" @click="onSubmit">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { loginApi } from '@/api/index'
export default {
  name: 'LoginView',
  data() {
    return {
      rules: {
           phone: [{ required: true, message: '请输入手机号/邮箱', trigger: 'blur' }],
           password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
         },  

      isRemember: false,
      form:{
        phone: '',
        password: '',
      },
      
      autoLogin: false,
      isLogin: false,
      checked: true,
      isChecked: false
    }
  },
  created() {
    //this.getLogin()
    const expiration = localStorage.getItem('autoLoginExpiration');
    if (expiration && new Date(expiration) > new Date()) {
      this.autoLogin = true;
      this.phone = localStorage.getItem('autoLoginUsername');
      this.password = localStorage.getItem('autoLoginPassword');
    }
  },
  mounted() {
    this.checkAutoLogin();
  },
  methods: {
    changeLogin() {
      this.isLogin = !this.isLogin
    },
    /* submit () {
      localStorage.setItem('token', '12313123')
      this.$router.push('/')
    }, */

    onSubmit() {
      if (!this.form.phone) {
        this.$message({ message: '请输入手机号或邮箱地址', type: 'error' })
      }
      if (this.form.phone) {
        if (!this.form.password) {
          this.$message({ message: '请输入密码', type: 'error' })
        }
      }

      if (this.autoLogin) {
        const expirationDate = new Date();
        expirationDate.setDate(expirationDate.getMinutes() + 1);
        localStorage.setItem('autoLoginUsername', this.form.phone);
        localStorage.setItem('autoLoginPassword', this.form.password);
        localStorage.setItem('autoLoginExpiration', expirationDate.toISOString());


      // 设置定时器，在1分钟后清除本地存储的信息
      setTimeout(() => {
        localStorage.removeItem('autoLoginUsername');
        localStorage.removeItem('autoLoginPassword');
        localStorage.removeItem('autoLoginExpiration');
      },1000 * 60 * 60); 
      }
     /*  else {
        localStorage.removeItem('autoLoginUsername');
        localStorage.removeItem('autoLoginPassword');
        localStorage.removeItem('autoLoginExpiration');
      } */

      let reg = /^\d+$/
      let type = reg.test(this.form.phone) ? 0 : 1
      let data = {
        login_type: type,
        password: this.form.password
      }
      if (type == 1) {
        data.email = this.email
      } else {
        data.phone = this.form.phone
      }
      console.log(data, "<<<<<<<<<<<<")
      loginApi(data).then(res => {
        localStorage.setItem("token", res.data.data.token)
        this.$store.commit("CHANGE_LOGIN", true)
        console.log(res.data)
        this.$message({
          message: res.data.msg,
          type: 'success',
          onClose: () => {
            this.$nextTick(() => {
              setTimeout(() => {
                this.$router.push({ path: '/' })
              })
            })
          }
        })
      })
    },

    checkAutoLogin() {
      const username = localStorage.getItem('autoLoginUsername');
      const password = localStorage.getItem('autoLoginPassword');
      const expiration = localStorage.getItem('autoLoginExpiration');
      if (username && password && expiration) {
        const expirationDate = new Date(expiration);
        if (expirationDate > new Date()) {
          this.form.phone = username;
          this.form.password = password;
          this.autoLogin = true;
        } else {
          localStorage.removeItem('autoLoginUsername');
          localStorage.removeItem('autoLoginPassword');
          localStorage.removeItem('autoLoginExpiration');
        }
      }
    }

  },

}

</script>

<style scoped lang="scss">
.login {
  // margin-top: 200px;
  background: url("@/assets/img/login-background.svg") no-repeat center;
  // height: 942px;
  width: 100%;
  background-size: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;

  .name {
    margin-top: 40px;
    font-size: 26px;
    font-weight: 600;
  }

  .small {
    font-size: 12px;
    color: rgba(0, 0, 0, 0.45);
    margin-top: 12px;
    margin-bottom: 40px;
  }

  .login_form {
    min-width: 260px;
    width: 368px;
    font-size: 14px;
    font-variant: tabular-nums;
    line-height: 1.5;

    .option {
      width: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .is_login {
        display: flex;
        align-items: center;
        cursor: pointer;

        &:hover {
          .icon_box {
            border-color: #04d1b3;
          }
        }

        .icon_box {
          width: 15px;
          height: 15px;
          border: 1px solid #d9d9d9;
          display: flex;
          justify-content: center;
          align-items: center;
          margin-right: 8px;
          transition: color 0.6s;

          .auto-login {
            border: none;
            /* 去掉默认边框 */
            outline: none;
            /* 去掉聚焦时的外边框 */
            background-color: white;
            /* 设置初始背景颜色 */
          }

          .auto-login:focus {
            background-color: green;
            /* 设置聚焦时的背景颜色为绿色 */
          }
        }

        .active {
          border-color: #04d1b3;
        }
      }

      .link {
        .link_item {
          transition: color 0.6s;
          color: #04d1b3;
          cursor: pointer;
          margin-left: 10px;
          font-size: 14px;
        }

        .link_item:hover {
          color: #28debc;
        }
      }
    }

    .el-button {
      width: 100%;
      background-color: #04d1b3;
      border-color: #04d1b3;
      text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.12);
      box-shadow: 0 2px 0 rgba(0, 0, 0, 0.045);
      font-size: 16px;
    }
  }
}


/*  ::v-deep .el-checkbox__input:hover,.el-checkbox__input:focus,.el-checkbox__input:active{
  background-color: green;
} */

::v-deep .el-checkbox__input.is-checked .el-checkbox__inner {
  background-color: green; /* 将选中时的填充颜色改为绿色 */
  color: white; /* 将选中时的字体颜色改为白色 */
  border-color: green;
}

::v-deep .el-checkbox__input.is-checked .el-checkbox__inner {
  background-color: #04d1b3; /* 将选中时的填充颜色改为绿色 */
  color: white; /* 将选中时的字体颜色改为白色 */
  border-color: #04d1b3;
}
</style>
